local Msg = require("core.Msg")
local Grid = require("bag.Grid")
local ActDefine = require("activity.Define")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local QPointExcel = require("excel.OperateActivity").qpoint
local ItemDefine = require("bag.ItemDefine")
local InnerDataManagerFW = require("innerdata.InnerDataManagerFW")
local InnerDataDefine = require("innerdata.Define")
local MailLogicFW =require("mail.MailLogicFW")
local OperateActLogicW = require("activity.operate.OperateActLogicW")
local HtmlUtil = require("common.HtmlUtil")
local Timer = require("core.Timer")
local Log = require("common.Log")
local OperateActFW = require("activity.OperateActFW")

QPOINT_LOTTERY = QPOINT_LOTTERY or {}
QPOINT_THIRD = QPOINT_THIRD or {} --前三名中奖号码

function getPersonInfo(human)
  local info = {}
  for i=1, #QPOINT_LOTTERY do
    if human._id == QPOINT_LOTTERY[i].uuid then
      info[#info + 1] = {}
      info[#info].num = QPOINT_LOTTERY[i].num
    end
  end
  return info
end

function setLotteryData()
  OperateActFW.onSetActDBData(ActDefine.OPERATEACT_TYPE_QPOINT, QPOINT_LOTTERY)
end

function getLotteryData()
  QPOINT_LOTTERY = OperateActFW.onGetActDBData(ActDefine.OPERATEACT_TYPE_QPOINT)
  if not QPOINT_LOTTERY then
    QPOINT_LOTTERY = {}
  end
  if not QPOINT_LOTTERY.base then
    QPOINT_LOTTERY.base = 0
  end
end

function open(human, id, type)
  if #QPOINT_LOTTERY == 0 then
    getLotteryData()
  end
  local sendMsg = Msg.gc.GC_YUNYING_QPOINT_INFO
  local activityNum
  if type == 2 then
    activityNum = OperateActFW.onGetTomorrowActRuleID(id or ActDefine.OPERATEACT_TYPE_QPOINT)
  else
    activityNum = OperateActFW.onGetCurActRuleID(id or ActDefine.OPERATEACT_TYPE_QPOINT)    
  end
  local activity = QPointExcel[activityNum]
  if not activity then
    return
  end
  sendMsg.third[0] = #activity.divide
  for i=1,#activity.divide do
    sendMsg.third[i].divide = activity.divide[i] 
    sendMsg.third[i].name = QPOINT_THIRD[i] and QPOINT_THIRD[i].name or ""
    sendMsg.third[i].num = QPOINT_THIRD[i] and QPOINT_THIRD[i].num or 0 
    sendMsg.third[i].uuid = QPOINT_THIRD[i] and QPOINT_THIRD[i].uuid or ""
  end
  local cache = {}
  Grid.create(cache, activity.reward[1], activity.reward[2])
  Grid.makeItem(cache, sendMsg.reward, 0, human)
  sendMsg.curPool = #QPOINT_LOTTERY * activity.needQPoint + ((QPOINT_LOTTERY.base ~= 0) and QPOINT_LOTTERY.base or activity.basePool)
  local info = getPersonInfo(human)
  sendMsg.buyTimes = #info
  sendMsg.buyNums[0] = #info
  for i=1, #info do
    sendMsg.buyNums[i] = info[i].num
  end
  sendMsg.type = type or 1
  sendMsg.giveYuanbao = activity.sendYuabao
  sendMsg.needQPoint = activity.needQPoint
  Msg.send(sendMsg, human.fd)
  OperateActLogicW.sendQPointRecord(human)
end
function getNum()
  return math.floor(Timer.now%1000000000)
end
--投注
function getReward(human)
  if #QPOINT_LOTTERY == 0 then
    getLotteryData()
  end
  local activityNum = OperateActFW.onGetCurActRuleID(ActDefine.OPERATEACT_TYPE_QPOINT)
  local activity = QPointExcel[activityNum]
  if not activity then
    return
  end
  local tmp = getPersonInfo(human)
  if #tmp >= 100 then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.YUNYING_QPOINT_LIMIT)
  end
  local len = #QPOINT_LOTTERY + 1
  QPOINT_LOTTERY[len] = {}
  QPOINT_LOTTERY[len].uuid = human._id
  QPOINT_LOTTERY[len].num = getNum()
  QPOINT_LOTTERY[len].name = human.name
  
  setLotteryData()
  local info = {}
  info.type = ActDefine.OPERATEACT_TYPE_QPOINT
  info.id = ItemDefine.ITEM_ID_YUANBAO
  info.cnt = activity.needQPoint
  OperateActLogicW.setRecord(human, nil, info)
  open(human)
  if #tmp == 0 then
    info.cnt = activity.sendYuabao
    InnerDataManagerFW.Send2LInnerData(human, InnerDataDefine.INNERDATA_TYPE_YUNYING_QPOINT, info)
    local str = string.format(Lang.YUNYING_QPOINT_BRO, HtmlUtil.fontName(human.name, human._id), info.cnt, human._id)
    Broadcast.sendBroadcast2World(Lang.UP + Lang.GROSSIP, str)
  end
  Log.write(Log.LOGID_OSS_YUNYING_QPOINT, human._id, human.account, human.name,human.db.lv)
  return (#tmp == 0) and true or false
end
--删除人
function clearHuman(uuid)
  for i=#QPOINT_LOTTERY, 1, -1 do
    if uuid == QPOINT_LOTTERY[i].uuid then      
      table.remove(QPOINT_LOTTERY, i)
    end
  end
end
--结算
function closeActivity()
  QPOINT_LOTTERY = {}
  QPOINT_THIRD = {}
end

function setReward()
  if #QPOINT_LOTTERY == 0 then
    return
  end
  local activityNum = OperateActFW.onGetCurActRuleID(ActDefine.OPERATEACT_TYPE_QPOINT)
  local activity = QPointExcel[activityNum]
  if not activity then
    return
  end
  
  local totalPool = #QPOINT_LOTTERY * activity.needQPoint + ((QPOINT_LOTTERY.base ~= 0) and QPOINT_LOTTERY.base or activity.basePool)

  for i=1, 10 do
    if #QPOINT_LOTTERY == 0 then
      break
    end
    local rand = math.random(1, #QPOINT_LOTTERY)
    local nGridList = {}
    local yuanbaoCnt = 0
    if i <= 3 then
      yuanbaoCnt = math.floor(totalPool * activity.divide[i] / 100)
      QPOINT_THIRD[i] = {}
      QPOINT_THIRD[i].name = QPOINT_LOTTERY[rand].name
      QPOINT_THIRD[i].num = QPOINT_LOTTERY[rand].num
      QPOINT_THIRD[i].uuid = QPOINT_LOTTERY[rand].uuid
    else
      yuanbaoCnt = math.floor(totalPool * 20 / 100 / 7)
    end
    nGridList[1] = Grid.create(nil, ItemDefine.ITEM_ID_YUANBAO, yuanbaoCnt, 1)
    local str = string.format("天降神运，恭喜你在幸运彩票中中了%d等奖,中奖号码%d,特发奖励给您", (i<=3) and i or 4, QPOINT_LOTTERY[rand].num)
    MailLogicFW.addSystemMessage(QPOINT_LOTTERY[rand].uuid, "幸运彩票", str, nGridList)
    clearHuman(QPOINT_LOTTERY[rand].uuid)
  end
  for i=#QPOINT_LOTTERY, 1, -1 do
    if #QPOINT_LOTTERY == 0 then
      break
    end
    local nGridList = {}
    nGridList[1] = Grid.create(nil, activity.reward[1], activity.reward[2], 1)
    if #QPOINT_LOTTERY ~= 0 and QPOINT_LOTTERY[i] then
      MailLogicFW.addSystemMessage(QPOINT_LOTTERY[i].uuid, "幸运彩票","感谢您参与幸运彩票，特发参与奖给你", nGridList)
    else
      break
    end
    clearHuman(QPOINT_LOTTERY[i].uuid)
  end  
  QPOINT_LOTTERY = {}
end

function checkEnough()
  if #QPOINT_LOTTERY == 0 then
    getLotteryData()
  end
  local activityNum = OperateActFW.onGetCurActRuleID(ActDefine.OPERATEACT_TYPE_QPOINT)
  local activity = QPointExcel[activityNum]
  if not activity then
    return
  end
  
  local totalPool = #QPOINT_LOTTERY * activity.needQPoint + activity.basePool
  if totalPool < activity.needAdd then
    totalPool = math.random(activity.minAmax[1] / 10, activity.minAmax[2] / 10) * 10
    QPOINT_LOTTERY.base = totalPool - #QPOINT_LOTTERY * activity.needQPoint
  end
end
